package com.lxd.service.impl;

import com.lxd.entity.dto.CommentDTO;
import com.lxd.entity.enums.NotifyEnum;
import com.lxd.entity.enums.PageSize;
import com.lxd.entity.po.*;
import com.lxd.entity.query.*;
import com.lxd.entity.vo.CommentVO;
import com.lxd.entity.vo.PaginationResultVO;
import com.lxd.mappers.*;
import com.lxd.redis.RedisComponent;
import com.lxd.service.CommentService;
import com.lxd.utils.CopyTools;
import com.lxd.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论表 业务接口实现
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper<Comment, CommentQuery> commentMapper;
    @Autowired
    private RedisComponent redis;
    @Resource
    private NotificationMapper<Notification, NotificationQuery> notificationMapper;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<Comment> findListByParam(CommentQuery param) {
        return this.commentMapper.selectList(param);
    }

    @Override
    public List<CommentDTO> findListByParam2(CommentQuery param) {
        List<CommentDTO> list = commentMapper.selectList2(param);
        return list;
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(CommentQuery param) {
        return this.commentMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<CommentVO> findListByPage(CommentQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        param.setOrderBy("status desc, like_count desc , created_at desc");
        List<CommentDTO> list = this.findListByParam2(param);
        List<CommentVO> listVO = new ArrayList<>(list.size());
        for (CommentDTO commentDTO : list) {
            if (commentDTO.getStatus() == 0) {
                continue;
            }
            //返回该条评论的总点赞数量
            String likeCount = redis.getLikeCount(commentDTO.getId().toString());

            CommentVO commentVO = CopyTools.copy(commentDTO, CommentVO.class);
            commentVO.setLikeCount(Integer.valueOf(likeCount));//从redis中获取最新点赞数量
            commentVO.setCommented(commentDTO.getActivityTitle() != null ? commentDTO.getActivityTitle() : commentDTO.getNewsTitle());
            listVO.add(commentVO);
        }
        PaginationResultVO<CommentVO> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), listVO);
        return result;
    }

    @Override
    public PaginationResultVO<CommentVO> AdminFindListByPage(CommentQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        param.setOrderBy("status desc, like_count desc, created_at desc");
        List<CommentDTO> list = this.findListByParam2(param);
        List<CommentVO> listVO = new ArrayList<>(list.size());
        for (CommentDTO commentDTO : list) {

            //返回该条评论的总点赞数量
            String likeCount = redis.getLikeCount(commentDTO.getId().toString());

            CommentVO commentVO = CopyTools.copy(commentDTO, CommentVO.class);
            commentVO.setLikeCount(Integer.valueOf(likeCount));//从redis中获取最新点赞数量
            commentVO.setCommented(commentDTO.getActivityTitle() != null ? commentDTO.getActivityTitle() : commentDTO.getNewsTitle());
            listVO.add(commentVO);
        }
        PaginationResultVO<CommentVO> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), listVO);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(Comment bean) {
        return this.commentMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<Comment> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.commentMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<Comment> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.commentMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(Comment bean, CommentQuery param) {
        StringTools.checkParam(param);
        return this.commentMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(CommentQuery param) {
        StringTools.checkParam(param);
        return this.commentMapper.deleteByParam(param);
    }

    /**
     * 根据Id获取对象
     */
    @Override
    public Comment getCommentById(Long id) {
        return this.commentMapper.selectById(id);
    }

    /**
     * 根据Id修改
     */
    @Override
    public Integer updateCommentById(CommentVO commentVO) {
        Comment comment = CopyTools.copy(commentVO, Comment.class);
        comment.setUpdatedAt(new Date());
        return this.commentMapper.updateById(comment, comment.getId());
    }

    /**
     * 根据Id删除
     */
    @Override
    public Integer deleteCommentById(Long id) {
        return this.commentMapper.deleteById(id);
    }

    /**
     * 根据NewsIdAndActivityId获取对象
     */
    @Override
    public Comment getCommentByNewsIdAndActivityId(Long newsId, Long activityId) {
        return this.commentMapper.selectByNewsIdAndActivityId(newsId, activityId);
    }

    /**
     * 根据NewsIdAndActivityId修改
     */
    @Override
    public Integer updateCommentByNewsIdAndActivityId(Comment bean, Long newsId, Long activityId) {
        return this.commentMapper.updateByNewsIdAndActivityId(bean, newsId, activityId);
    }

    /**
     * 根据NewsIdAndActivityId删除
     */
    @Override
    public Integer deleteCommentByNewsIdAndActivityId(Long newsId, Long activityId) {
        return this.commentMapper.deleteByNewsIdAndActivityId(newsId, activityId);
    }

    @Override
    @Transactional
    public Integer updateLikeCountById(Long commentId, Long likeCount) {
        Integer i = commentMapper.updateLikeCountById(commentId, likeCount);
        notifyToLikedUser(commentId, likeCount);
        //通知被点赞者
        return i;

    }

    public void notifyToLikedUser(Long commentId, Long likeCount) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) {
            return;
        }
        //通知被点赞者
        String userId = comment.getUserId().toString();
        Notification notification = new Notification();
        notification.setUserId(Long.valueOf(userId));
        notification.setIsRead(0);
        notification.setContent(NotifyEnum.LikeCountNotify.getContent());
        notification.setTitle(NotifyEnum.LikeCountNotify.getTitle());
        notification.setCreatedAt(new Date());
        notificationMapper.insert(notification);
        //如果点赞量为10, 或者100的倍数，则再发送通知
        if (likeCount == 10) {
            notification.setContent(NotifyEnum.LikeCountTenNotify.getContent());
            notification.setTitle(NotifyEnum.LikeCountTenNotify.getTitle());
        }
        else if (likeCount % 100 == 0) {
            notification.setContent("您已累计收集" + likeCount + "个赞！！！");
            notification.setTitle(NotifyEnum.LikeCountHundredNotify.getTitle());
        }
        notificationMapper.insert(notification);
    }
}